﻿using System;
using System.Collections;

namespace _01_FlyweightModel
{
    class Program
    {
        static void Main(string[] args)
        {
            int extrinsicstate = 22; // 外部状态

            FlyweightFactory flyweightFactory = new FlyweightFactory();

            Flyweight fx = flyweightFactory.GetFlyweight("X");
            fx.Operation(--extrinsicstate);

            Flyweight fy = flyweightFactory.GetFlyweight("Y");
            fy.Operation(--extrinsicstate);

            Flyweight fz = flyweightFactory.GetFlyweight("Z");
            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight unsharedConcreteFlyweight = new UnsharedConcreteFlyweight();
            unsharedConcreteFlyweight.Operation(--extrinsicstate);

            Console.Read();

        }
    }

    // 享元类
    abstract class Flyweight
    {
        public abstract void Operation(int extrinsicstate);
    }

    class ConcreteFlyweight : Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("具体Flyweight：" + extrinsicstate);
        }
    }
    // 那些不需要共享的子类
    class UnsharedConcreteFlyweight : Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("不共享的具体Flyweight：" + extrinsicstate);
        }
    }
    // 享元工厂类
    class FlyweightFactory
    {
        private Hashtable flyweights = new Hashtable();
        public FlyweightFactory()
        {
            flyweights.Add("X", new ConcreteFlyweight());
            flyweights.Add("Y", new ConcreteFlyweight());
            flyweights.Add("Z", new ConcreteFlyweight());
        }
        public Flyweight GetFlyweight(string key)
        {
            return (Flyweight)flyweights[key];
        }
    }
}
